package com.aysafety.eventmodel.corn;

import com.aysafety.eventmodel.common.DateUtils;
import com.aysafety.eventmodel.service.microseism.MicroseismService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: iotDataPlatform
 * @description: 微震
 * @author: ChengQi
 * @create: 2018-12-26 10:42
 **/
//@Component
public class MicroseismTask {

    @Autowired MicroseismService microseismService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * @Description: 频次、频次偏差、日总能量、日最大能量、日平均能量
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/3/7
     */
//    @Scheduled(cron = "0 0 2 1/1 * ? ")
    public void task1() {
        logger.info("正在计算频次...");
        String currentTime = DateUtils.getTime();
        try {
            microseismService.frequency(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成频次计算...");
        logger.info("频次计算完成...currentTime:" + currentTime);
    }

    /**
     * @Description: 能量偏差值、频次比
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/3/8
     */
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void task2() {
        String currentTime = DateUtils.getTime();
        logger.info("正在计算能量平均值和频次比currentTime..." + currentTime);
        try {
            microseismService.frequency(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成能量平均、频次比计算currentTime..." + currentTime);
    }

    /**
     * @Description: 离散度
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/3/8
     */
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void task3() {
        String currentTime = DateUtils.getTime();
        logger.info("正在计算离散度currentTime..." + currentTime);
        try {
            microseismService.dispersion(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成离散度计算currentTime..." + currentTime);
    }

    /** 
    * @Description: 综合预警计算实时的W值
    * @Param: [] 
    * @return: void 
    * @Author: ChengQi 
    * @Date: 2019/3/18 
    */ 
    //    @Scheduled(cron = "0 0/1 * * * ? ")
    public void task4() {
        String currentTime = DateUtils.getTime();
        logger.info("正在计算综合预警WcurrentTime..." + currentTime);
        try {
            microseismService.warning(currentTime);
        } catch (Throwable throwable) {
            logger.error("frequency error,currentTime:" + currentTime);
        }
        logger.info("完成综合预警W计算currentTime..." + currentTime);
    }


    //@Scheduled(cron = "30 34 15 20 * ? ")
    public void task4History() {
        logger.info("正在计算综合预警W...历史");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-03-21 13:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2016-06-07 00:00:00");
                Date endDate = dateFormat1.parse("2019-03-16 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    microseismService.warning(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


//    @Scheduled(cron = "0 20 15 15 * ? ")
    public void task3History() {
        logger.info("正在计算离散度,历史...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-03-16 13:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2016-06-06 09:00:00");
                Date endDate = dateFormat1.parse("2019-03-16 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    microseismService.dispersion(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 60);
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成离散度计算...历史");
    }


    /**
     * @Description: 能量平均值、频次偏差
     * @Param: []
     * @return: void
     * @Author: ChengQi
     * @Date: 2019/3/7
     */
//    @Scheduled(cron = "30 33 14 20 * ? ")
    public void task2History() {
        logger.info("正在计算能量平均值和频次比,历史...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-03-19 13:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2017-11-09 05:00:00");
                Date endDate = dateFormat1.parse("2019-03-19 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    microseismService.task2(currentTime);
                    c.setTime(date);
                    c.add(Calendar.MINUTE, 10); // 日期加1天
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("完成能量平均值和频次比计算...历史");
    }


//    @Scheduled(cron = "0 35 11 18 * ? ")
    public void task1History() {
        logger.info("正在计算频次...");
        try {
            String s = DateUtils.getTime();
            if (!DateUtils.compareDate(s, "2019-03-19 00:00:00")) {
                Calendar c = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 开始时间必须小于结束时间
                Date beginDate = dateFormat1.parse("2016-06-06 09:00:00");
                Date endDate = dateFormat1.parse("2019-03-19 00:00:00");
                Date date = beginDate;
                while (!date.equals(endDate)) {
                    String currentTime = DateUtils.getTime(date);
                    microseismService.frequency(currentTime);
                    logger.info("频次计算完成...currentTime:" + currentTime);
                    c.setTime(date);
                    c.add(Calendar.DATE, 1); // 日期加1天
                    date = c.getTime();
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

}
